#include "dtu_config.h"
#include "dtu_ini.h"
#include "dtu_log.h"
#include "dtu_memory.h"
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
// #include <cjson/cjson.h>  // TODO: 添加cJSON依赖

/* 全局配置管理器 */
static config_manager_t g_config_manager = {0};



/* 内部函数声明 */
static dtu_error_t load_config_from_file(const char* filename);
static dtu_error_t save_config_to_file(const char* filename);

dtu_error_t dtu_config_init(const char* config_file)
{
    if (g_config_manager.initialized) {
        return DTU_OK;
    }

    memset(&g_config_manager, 0, sizeof(config_manager_t));

    /* 设置配置文件路径 */
    if (config_file) {
        /* 使用命令行指定的配置文件 */
        DTU_STRNCPY(g_config_manager.config_file, config_file, sizeof(g_config_manager.config_file));
    } else {
        /* 智能查找config/dtu_config.ini路径 */
        const char* possible_paths[] = {
            "config/dtu_config.ini",           /* 当前目录下的config */
            "../config/dtu_config.ini",        /* 上级目录的config (CLion调试时) */
            "../../config/dtu_config.ini",     /* 上上级目录的config */
            NULL
        };

        bool found = false;
        for (int i = 0; possible_paths[i] != NULL; i++) {
            if (access(possible_paths[i], F_OK) == 0) {
                DTU_STRNCPY(g_config_manager.config_file, possible_paths[i], sizeof(g_config_manager.config_file));
                found = true;
                break;
            }
        }

        if (!found) {
            /* 如果都找不到，使用默认路径（会自动创建） */
            DTU_STRNCPY(g_config_manager.config_file, "config/dtu_config.ini", sizeof(g_config_manager.config_file));
        }
    }

    /* 初始化互斥锁 */
    if (pthread_mutex_init(&g_config_manager.mutex, NULL) != 0) {
        return DTU_ERROR;
    }

    /* 初始化配置结构为零 */
    memset(&g_config_manager.config, 0, sizeof(dtu_config_t));
    g_config_manager.config.device_info.manufacture_date = time(NULL);

    g_config_manager.initialized = true;

    DTU_LOG_INFO("Config manager initialized with file: %s", g_config_manager.config_file);
    DTU_LOG_INFO("Current working directory: %s", getcwd(NULL, 0) ? getcwd(NULL, 0) : "unknown");
    return DTU_OK;
}

void dtu_config_cleanup(void)
{
    if (!g_config_manager.initialized) {
        return;
    }

    pthread_mutex_lock(&g_config_manager.mutex);

    g_config_manager.initialized = false;

    pthread_mutex_unlock(&g_config_manager.mutex);
    pthread_mutex_destroy(&g_config_manager.mutex);

    DTU_LOG_INFO("Config manager cleanup completed");
}

dtu_error_t dtu_config_load(void)
{
    if (!g_config_manager.initialized) {
        return DTU_ERROR;
    }

    pthread_mutex_lock(&g_config_manager.mutex);

    dtu_error_t ret = load_config_from_file(g_config_manager.config_file);
    if (ret == DTU_OK) {
        DTU_LOG_INFO("Configuration loaded successfully from %s", g_config_manager.config_file);
    } else {
        DTU_LOG_WARN("Failed to load configuration from %s, using defaults", g_config_manager.config_file);
    }

    pthread_mutex_unlock(&g_config_manager.mutex);
    return ret;
}

dtu_error_t dtu_config_save(void)
{
    if (!g_config_manager.initialized) {
        return DTU_ERROR;
    }

    pthread_mutex_lock(&g_config_manager.mutex);

    dtu_error_t ret = save_config_to_file(g_config_manager.config_file);
    if (ret == DTU_OK) {
        DTU_LOG_INFO("Configuration saved successfully to %s", g_config_manager.config_file);
    } else {
        DTU_LOG_ERROR("Failed to save configuration to %s", g_config_manager.config_file);
    }

    pthread_mutex_unlock(&g_config_manager.mutex);
    return ret;
}

dtu_error_t dtu_config_reload(void)
{
    if (!g_config_manager.initialized) {
        return DTU_ERROR;
    }

    DTU_LOG_INFO("Reloading configuration...");
    return dtu_config_load();
}

dtu_error_t dtu_config_reset_to_default(void)
{
    if (!g_config_manager.initialized) {
        return DTU_ERROR;
    }

    pthread_mutex_lock(&g_config_manager.mutex);

    /* 重新从INI文件加载默认配置 */
    load_config_from_file(g_config_manager.config_file);
    g_config_manager.config.device_info.manufacture_date = time(NULL);

    pthread_mutex_unlock(&g_config_manager.mutex);

    DTU_LOG_INFO("Configuration reset to defaults");
    return DTU_OK;
}

const dtu_config_t* dtu_config_get(void)
{
    if (!g_config_manager.initialized) {
        return NULL;
    }

    return &g_config_manager.config;
}

dtu_error_t dtu_config_set(const dtu_config_t* config)
{
    if (!g_config_manager.initialized || !config) {
        return DTU_ERROR_INVALID_PARAM;
    }

    pthread_mutex_lock(&g_config_manager.mutex);

    g_config_manager.config = *config;

    pthread_mutex_unlock(&g_config_manager.mutex);

    DTU_LOG_INFO("Configuration updated");
    return DTU_OK;
}



/* 内部函数实现 */
static dtu_error_t load_config_from_file(const char* filename)
{
    if (!dtu_ini_file_exists(filename)) {
        DTU_LOG_WARN("Config file not found, creating default: %s", filename);
        dtu_ini_create_default(filename);
    }

    /* 从INI文件加载设备信息 */
    dtu_ini_get_string(filename, "device_info", "device_id", "DTU001",
                       g_config_manager.config.device_info.device_id,
                       sizeof(g_config_manager.config.device_info.device_id));
    dtu_ini_get_string(filename, "device_info", "serial_number", "SN20240101001",
                       g_config_manager.config.device_info.serial_number,
                       sizeof(g_config_manager.config.device_info.serial_number));
    g_config_manager.config.device_info.model = dtu_ini_get_int(filename, "device_info", "model", 1);
    dtu_ini_get_string(filename, "device_info", "firmware_version", "1.0.0",
                       g_config_manager.config.device_info.firmware_version,
                       sizeof(g_config_manager.config.device_info.firmware_version));
    dtu_ini_get_string(filename, "device_info", "hardware_version", "HW1.0",
                       g_config_manager.config.device_info.hardware_version,
                       sizeof(g_config_manager.config.device_info.hardware_version));

    /* 根据编译时定义自动设置build_version */
    const char* default_build_version = "debug";
#ifdef BUILD_ETHERNET_ONLY
    default_build_version = "ethernet";
#elif defined(BUILD_WIFI_ETHERNET)
    default_build_version = "wifi";
#elif defined(BUILD_4G_ETHERNET)
    default_build_version = "4g";
#endif

    dtu_ini_get_string(filename, "device_info", "build_version", default_build_version,
                       g_config_manager.config.device_info.build_version,
                       sizeof(g_config_manager.config.device_info.build_version));

    /* 加载RS485配置 */
    dtu_ini_get_string(filename, "rs485_config", "device", "/dev/ttyUSB0",
                       g_config_manager.config.rs485_config.device,
                       sizeof(g_config_manager.config.rs485_config.device));
    g_config_manager.config.rs485_config.baudrate = dtu_ini_get_int(filename, "rs485_config", "baudrate", 9600);
    g_config_manager.config.rs485_config.databits = dtu_ini_get_int(filename, "rs485_config", "databits", 8);
    g_config_manager.config.rs485_config.stopbits = dtu_ini_get_int(filename, "rs485_config", "stopbits", 1);
    char parity_str[4];
    dtu_ini_get_string(filename, "rs485_config", "parity", "N", parity_str, sizeof(parity_str));
    g_config_manager.config.rs485_config.parity = parity_str[0];
    g_config_manager.config.rs485_config.timeout = dtu_ini_get_int(filename, "rs485_config", "timeout", 1000);

    /* 加载RS232配置 */
    dtu_ini_get_string(filename, "rs232_config", "device", "/dev/ttyUSB1",
                       g_config_manager.config.rs232_config.device,
                       sizeof(g_config_manager.config.rs232_config.device));
    g_config_manager.config.rs232_config.baudrate = dtu_ini_get_int(filename, "rs232_config", "baudrate", 9600);
    g_config_manager.config.rs232_config.databits = dtu_ini_get_int(filename, "rs232_config", "databits", 8);
    g_config_manager.config.rs232_config.stopbits = dtu_ini_get_int(filename, "rs232_config", "stopbits", 1);
    dtu_ini_get_string(filename, "rs232_config", "parity", "N", parity_str, sizeof(parity_str));
    g_config_manager.config.rs232_config.parity = parity_str[0];
    g_config_manager.config.rs232_config.timeout = dtu_ini_get_int(filename, "rs232_config", "timeout", 1000);

    /* 加载网络配置 */
    g_config_manager.config.ethernet_config.dhcp_enable = dtu_ini_get_bool(filename, "ethernet_config", "dhcp_enable", true);
    dtu_ini_get_string(filename, "ethernet_config", "ip", "192.168.1.100",
                       g_config_manager.config.ethernet_config.ip,
                       sizeof(g_config_manager.config.ethernet_config.ip));
    dtu_ini_get_string(filename, "ethernet_config", "netmask", "255.255.255.0",
                       g_config_manager.config.ethernet_config.netmask,
                       sizeof(g_config_manager.config.ethernet_config.netmask));
    dtu_ini_get_string(filename, "ethernet_config", "gateway", "192.168.1.1",
                       g_config_manager.config.ethernet_config.gateway,
                       sizeof(g_config_manager.config.ethernet_config.gateway));
    dtu_ini_get_string(filename, "ethernet_config", "dns1", "8.8.8.8",
                       g_config_manager.config.ethernet_config.dns1,
                       sizeof(g_config_manager.config.ethernet_config.dns1));
    dtu_ini_get_string(filename, "ethernet_config", "dns2", "8.8.4.4",
                       g_config_manager.config.ethernet_config.dns2,
                       sizeof(g_config_manager.config.ethernet_config.dns2));

    /* 加载WiFi配置 */
    dtu_ini_get_string(filename, "wifi_config", "ssid", "",
                       g_config_manager.config.wifi_config.ssid,
                       sizeof(g_config_manager.config.wifi_config.ssid));
    dtu_ini_get_string(filename, "wifi_config", "password", "",
                       g_config_manager.config.wifi_config.password,
                       sizeof(g_config_manager.config.wifi_config.password));
    g_config_manager.config.wifi_config.security_type = dtu_ini_get_int(filename, "wifi_config", "security_type", 2);

    /* 加载LTE配置 */
    dtu_ini_get_string(filename, "lte_config", "apn", "cmnet",
                       g_config_manager.config.lte_config.apn,
                       sizeof(g_config_manager.config.lte_config.apn));
    dtu_ini_get_string(filename, "lte_config", "username", "",
                       g_config_manager.config.lte_config.username,
                       sizeof(g_config_manager.config.lte_config.username));
    dtu_ini_get_string(filename, "lte_config", "password", "",
                       g_config_manager.config.lte_config.password,
                       sizeof(g_config_manager.config.lte_config.password));
    g_config_manager.config.lte_config.pin_code = dtu_ini_get_int(filename, "lte_config", "pin_code", 0);

    /* 加载采集配置 */
    g_config_manager.config.collect_config.interval = dtu_ini_get_int(filename, "collect_config", "interval", 30);
    g_config_manager.config.collect_config.auto_collect = dtu_ini_get_bool(filename, "collect_config", "auto_collect", true);
    g_config_manager.config.collect_config.retry_count = dtu_ini_get_int(filename, "collect_config", "retry_count", 3);
    g_config_manager.config.collect_config.retry_interval = dtu_ini_get_int(filename, "collect_config", "retry_interval", 5);

    /* 加载推送配置 */
    g_config_manager.config.push_config.protocol = dtu_ini_get_int(filename, "push_config", "protocol", 0);
    g_config_manager.config.push_config.format = dtu_ini_get_int(filename, "push_config", "format", 0);
    dtu_ini_get_string(filename, "push_config", "server_host", "mqtt.example.com",
                       g_config_manager.config.push_config.server_host,
                       sizeof(g_config_manager.config.push_config.server_host));
    g_config_manager.config.push_config.server_port = dtu_ini_get_int(filename, "push_config", "server_port", 1883);
    dtu_ini_get_string(filename, "push_config", "username", "",
                       g_config_manager.config.push_config.username,
                       sizeof(g_config_manager.config.push_config.username));
    dtu_ini_get_string(filename, "push_config", "password", "",
                       g_config_manager.config.push_config.password,
                       sizeof(g_config_manager.config.push_config.password));
    dtu_ini_get_string(filename, "push_config", "topic", "dtu/data",
                       g_config_manager.config.push_config.topic,
                       sizeof(g_config_manager.config.push_config.topic));
    dtu_ini_get_string(filename, "push_config", "client_id", "DTU001",
                       g_config_manager.config.push_config.client_id,
                       sizeof(g_config_manager.config.push_config.client_id));
    g_config_manager.config.push_config.timeout = dtu_ini_get_int(filename, "push_config", "timeout", 30);
    g_config_manager.config.push_config.retry_count = dtu_ini_get_int(filename, "push_config", "retry_count", 3);
    g_config_manager.config.push_config.retry_interval = dtu_ini_get_int(filename, "push_config", "retry_interval", 5);
    g_config_manager.config.push_config.enable_ssl = dtu_ini_get_bool(filename, "push_config", "enable_ssl", false);
    g_config_manager.config.push_config.enable_compression = dtu_ini_get_bool(filename, "push_config", "enable_compression", false);
    g_config_manager.config.push_config.keepalive = dtu_ini_get_int(filename, "push_config", "keepalive", 60);
    g_config_manager.config.push_config.clean_session = dtu_ini_get_bool(filename, "push_config", "clean_session", true);

    /* 加载MQTT订阅配置 */
    dtu_ini_get_string(filename, "mqtt_subscribe", "topic", "dtu/DTU001/command",
                       g_config_manager.config.mqtt_subscribe_config.topic,
                       sizeof(g_config_manager.config.mqtt_subscribe_config.topic));
    g_config_manager.config.mqtt_subscribe_config.qos = dtu_ini_get_int(filename, "mqtt_subscribe", "qos", 1);

    /* 加载Modbus设备配置 - 新的简化格式 */
    g_config_manager.config.modbus_config.device_count = 0;
    g_config_manager.config.modbus_config.max_devices = dtu_ini_get_int(filename, "modbus_config", "max_devices", 16);

    /* 首先尝试加载新格式（AT指令字符串） */
    for (int i = 0; i < 16; i++) {
        char key[64];
        snprintf(key, sizeof(key), "modbus_config.pollstr_%d", i + 1);

        dtu_ini_get_string(filename, "modbus_config", key + strlen("modbus_config."), "",
                           g_config_manager.config.modbus_config.pollstr_commands[i],
                           sizeof(g_config_manager.config.modbus_config.pollstr_commands[i]));

        if (strlen(g_config_manager.config.modbus_config.pollstr_commands[i]) > 0) {
            g_config_manager.config.modbus_config.device_count = i + 1;
        }
    }

    /* 如果新格式没有数据，则加载旧格式并转换（兼容性） */
    if (g_config_manager.config.modbus_config.device_count == 0) {
        int old_device_count = dtu_ini_get_int(filename, "modbus_device_count", "count", 1);

        for (int i = 0; i < old_device_count && i < 16; i++) {
            char section[32];
            snprintf(section, sizeof(section), "modbus_device_%d", i + 1);

            g_config_manager.config.modbus_config.devices[i].id = dtu_ini_get_int(filename, section, "id", i + 1);
            dtu_ini_get_string(filename, section, "name", "设备1",
                               g_config_manager.config.modbus_config.devices[i].name,
                               sizeof(g_config_manager.config.modbus_config.devices[i].name));
            g_config_manager.config.modbus_config.devices[i].enabled = dtu_ini_get_bool(filename, section, "enabled", true);
            g_config_manager.config.modbus_config.devices[i].crc_enable = dtu_ini_get_bool(filename, section, "crc_enable", true);
            dtu_ini_get_string(filename, section, "poll_string", "010300000001",
                               g_config_manager.config.modbus_config.devices[i].poll_string,
                               sizeof(g_config_manager.config.modbus_config.devices[i].poll_string));
            dtu_ini_get_string(filename, section, "serial_port", "rs485",
                               g_config_manager.config.modbus_config.devices[i].serial_port,
                               sizeof(g_config_manager.config.modbus_config.devices[i].serial_port));
            dtu_ini_get_string(filename, section, "description", "Modbus设备",
                               g_config_manager.config.modbus_config.devices[i].description,
                               sizeof(g_config_manager.config.modbus_config.devices[i].description));

            /* 将旧格式转换为新格式的AT指令字符串 */
            snprintf(g_config_manager.config.modbus_config.pollstr_commands[i],
                     sizeof(g_config_manager.config.modbus_config.pollstr_commands[i]),
                     "AT+POLLSTR=%d,%d,%d,%s",
                     g_config_manager.config.modbus_config.devices[i].id,
                     g_config_manager.config.modbus_config.devices[i].enabled ? 1 : 0,
                     g_config_manager.config.modbus_config.devices[i].crc_enable ? 1 : 0,
                     g_config_manager.config.modbus_config.devices[i].poll_string);
        }

        g_config_manager.config.modbus_config.device_count = old_device_count;
    }

    /* 加载Modbus测试配置 */
    g_config_manager.config.modbus_test_config.device_address = dtu_ini_get_int(filename, "modbus_test_config", "device_address", 1);
    g_config_manager.config.modbus_test_config.function_code = dtu_ini_get_int(filename, "modbus_test_config", "function_code", 3);
    g_config_manager.config.modbus_test_config.start_address = dtu_ini_get_int(filename, "modbus_test_config", "start_address", 3);
    g_config_manager.config.modbus_test_config.register_count = dtu_ini_get_int(filename, "modbus_test_config", "register_count", 1);
    dtu_ini_get_string(filename, "modbus_test_config", "serial_port", "rs485",
                       g_config_manager.config.modbus_test_config.serial_port,
                       sizeof(g_config_manager.config.modbus_test_config.serial_port));

    DTU_LOG_INFO("Configuration loaded from %s", filename);
    return DTU_OK;
}

static dtu_error_t save_config_to_file(const char* filename)
{
    const dtu_config_t* config = &g_config_manager.config;

    DTU_LOG_INFO("Saving configuration to file: %s", filename);
    DTU_LOG_INFO("Current working directory: %s", getcwd(NULL, 0) ? getcwd(NULL, 0) : "unknown");

    /* 保存设备信息 */
    dtu_ini_set_string(filename, "device_info", "device_id", config->device_info.device_id);
    dtu_ini_set_string(filename, "device_info", "serial_number", config->device_info.serial_number);
    dtu_ini_set_int(filename, "device_info", "model", config->device_info.model);
    dtu_ini_set_string(filename, "device_info", "firmware_version", config->device_info.firmware_version);
    dtu_ini_set_string(filename, "device_info", "hardware_version", config->device_info.hardware_version);
    dtu_ini_set_string(filename, "device_info", "build_version", config->device_info.build_version);

    /* 保存RS485配置 */
    dtu_ini_set_string(filename, "rs485_config", "device", config->rs485_config.device);
    dtu_ini_set_int(filename, "rs485_config", "baudrate", config->rs485_config.baudrate);
    dtu_ini_set_int(filename, "rs485_config", "databits", config->rs485_config.databits);
    dtu_ini_set_int(filename, "rs485_config", "stopbits", config->rs485_config.stopbits);
    char parity_str[2] = {config->rs485_config.parity, '\0'};
    dtu_ini_set_string(filename, "rs485_config", "parity", parity_str);
    dtu_ini_set_int(filename, "rs485_config", "timeout", config->rs485_config.timeout);

    /* 保存RS232配置 */
    dtu_ini_set_string(filename, "rs232_config", "device", config->rs232_config.device);
    dtu_ini_set_int(filename, "rs232_config", "baudrate", config->rs232_config.baudrate);
    dtu_ini_set_int(filename, "rs232_config", "databits", config->rs232_config.databits);
    dtu_ini_set_int(filename, "rs232_config", "stopbits", config->rs232_config.stopbits);
    parity_str[0] = config->rs232_config.parity;
    dtu_ini_set_string(filename, "rs232_config", "parity", parity_str);
    dtu_ini_set_int(filename, "rs232_config", "timeout", config->rs232_config.timeout);

    /* 保存网络配置 */
    dtu_ini_set_bool(filename, "ethernet_config", "dhcp_enable", config->ethernet_config.dhcp_enable);
    dtu_ini_set_string(filename, "ethernet_config", "ip", config->ethernet_config.ip);
    dtu_ini_set_string(filename, "ethernet_config", "netmask", config->ethernet_config.netmask);
    dtu_ini_set_string(filename, "ethernet_config", "gateway", config->ethernet_config.gateway);
    dtu_ini_set_string(filename, "ethernet_config", "dns1", config->ethernet_config.dns1);
    dtu_ini_set_string(filename, "ethernet_config", "dns2", config->ethernet_config.dns2);

    dtu_ini_set_string(filename, "wifi_config", "ssid", config->wifi_config.ssid);
    dtu_ini_set_string(filename, "wifi_config", "password", config->wifi_config.password);
    dtu_ini_set_int(filename, "wifi_config", "security_type", config->wifi_config.security_type);

    dtu_ini_set_string(filename, "lte_config", "apn", config->lte_config.apn);
    dtu_ini_set_string(filename, "lte_config", "username", config->lte_config.username);
    dtu_ini_set_string(filename, "lte_config", "password", config->lte_config.password);
    dtu_ini_set_int(filename, "lte_config", "pin_code", config->lte_config.pin_code);

    /* 保存采集配置 */
    dtu_ini_set_int(filename, "collect_config", "interval", config->collect_config.interval);
    dtu_ini_set_bool(filename, "collect_config", "auto_collect", config->collect_config.auto_collect);
    dtu_ini_set_int(filename, "collect_config", "retry_count", config->collect_config.retry_count);
    dtu_ini_set_int(filename, "collect_config", "retry_interval", config->collect_config.retry_interval);

    /* 保存推送配置 */
    dtu_ini_set_int(filename, "push_config", "protocol", config->push_config.protocol);
    dtu_ini_set_int(filename, "push_config", "format", config->push_config.format);
    dtu_ini_set_string(filename, "push_config", "server_host", config->push_config.server_host);
    dtu_ini_set_int(filename, "push_config", "server_port", config->push_config.server_port);
    dtu_ini_set_string(filename, "push_config", "username", config->push_config.username);
    dtu_ini_set_string(filename, "push_config", "password", config->push_config.password);
    dtu_ini_set_string(filename, "push_config", "topic", config->push_config.topic);
    dtu_ini_set_string(filename, "push_config", "client_id", config->push_config.client_id);
    dtu_ini_set_int(filename, "push_config", "timeout", config->push_config.timeout);
    dtu_ini_set_int(filename, "push_config", "retry_count", config->push_config.retry_count);
    dtu_ini_set_int(filename, "push_config", "retry_interval", config->push_config.retry_interval);
    dtu_ini_set_bool(filename, "push_config", "enable_ssl", config->push_config.enable_ssl);
    dtu_ini_set_bool(filename, "push_config", "enable_compression", config->push_config.enable_compression);
    dtu_ini_set_int(filename, "push_config", "keepalive", config->push_config.keepalive);
    dtu_ini_set_bool(filename, "push_config", "clean_session", config->push_config.clean_session);

    /* 保存Modbus设备配置 - 新的简化格式 */
    dtu_ini_set_int(filename, "modbus_config", "device_count", config->modbus_config.device_count);
    dtu_ini_set_int(filename, "modbus_config", "max_devices", config->modbus_config.max_devices);

    /* 保存AT指令字符串格式 */
    for (int i = 0; i < 16; i++) {
        char key[64];
        snprintf(key, sizeof(key), "pollstr_%d", i + 1);

        if (strlen(config->modbus_config.pollstr_commands[i]) > 0) {
            dtu_ini_set_string(filename, "modbus_config", key, config->modbus_config.pollstr_commands[i]);
        }
    }

    /* 为了兼容性，如果旧格式数据存在，也保存旧格式（可选） */
    /* 这部分可以在未来版本中移除 */

    /* 保存Modbus测试配置 */
    dtu_ini_set_int(filename, "modbus_test_config", "device_address", config->modbus_test_config.device_address);
    dtu_ini_set_int(filename, "modbus_test_config", "function_code", config->modbus_test_config.function_code);
    dtu_ini_set_int(filename, "modbus_test_config", "start_address", config->modbus_test_config.start_address);
    dtu_ini_set_int(filename, "modbus_test_config", "register_count", config->modbus_test_config.register_count);
    dtu_ini_set_string(filename, "modbus_test_config", "serial_port", config->modbus_test_config.serial_port);

    return DTU_OK;
}






